package com.wnhz.work.jy.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.OrderAdapter;
import com.wnhz.work.jy.bean.PurchaseCodeResult;
import com.wnhz.work.jy.bean.ShoppingBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PurchaseCode extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "PurchaseCode";
    private static final int PAY_PASSWORK = 624;
    private String orderId;
    private TextView title;
    private String pid;//柜员id
    private PurchaseCodeResult purchaseCodeResult;
    private OrderAdapter adapter;
    private ArrayList<ShoppingBean> list = new ArrayList<>();
    private ListView listView;
    private TextView tv_total_prices;
    private TextView rc_msg;
    private TextView bank_card_number;
    private TextView tv_phone;
    private TextView tv_name;
    private TextView view_main_right_text;
    private TextView bankSerial;
    private View lay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase_code);
        orderId = getIntent().getStringExtra("orderId");
        pid = getIntent().getStringExtra("pid");
        tv_total_prices = (TextView) findViewById(R.id.tv_total_prices);
        rc_msg = (TextView) findViewById(R.id.rc_msg);
        bank_card_number = (TextView) findViewById(R.id.bank_card_number);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_name = (TextView) findViewById(R.id.tv_name);
        bankSerial = (TextView) findViewById(R.id.bankSerial);
        lay = findViewById(R.id.lay);
        initTitle();
        findViewById(R.id.btn).setOnClickListener(this);
        findViewById(R.id.rl_coupon).setOnClickListener(this);
        listView = (ListView) findViewById(R.id.listView);
        adapter = new OrderAdapter(PurchaseCode.this, list);
        listView.setAdapter(adapter);
        getData();
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        view_main_right_text = (TextView) findViewById(R.id.view_main_right_text);

        title.setText("支付");
        view_main_right_text.setText("取消");
        view_main_right_text.setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_left).setVisibility(View.GONE);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
    }

    /**
     * # 获取用户id
     * $user_id=I('post.userId');                                            //用户id
     * #获取订单信息
     * $pid=I('post.pid');                                                   //柜员id
     * $guiyuanOrderid=I('post.guiyuanOrderid');                             //柜员生成订单号
     */
    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("guiyuanOrderid", orderId);
        map.put("pid", pid);
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.ZHIFUEWM, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(re)) {
                        lay.setVisibility(View.VISIBLE);

                        purchaseCodeResult = new PurchaseCodeResult();
                        purchaseCodeResult = gson.fromJson(info, PurchaseCodeResult.class);
                        list.clear();
                        list.addAll(purchaseCodeResult.getGoodsData());
                        tv_name.setText(purchaseCodeResult.getOrder().getTrueName());
                        tv_phone.setText(purchaseCodeResult.getOrder().getTelephone());
                        bank_card_number.setText(purchaseCodeResult.getOrder().getCertificate_no());
                        rc_msg.setText(purchaseCodeResult.getOrder().getMessage());
                        bankSerial.setText(purchaseCodeResult.getOrder().getBankSerial());
                        tv_total_prices.setText("¥" + purchaseCodeResult.getAllprice());
                        adapter.notifyDataSetChanged();
                    }else {
                        MyToast(info);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_right:
                startActivity(new Intent(PurchaseCode.this, MyMain.class).putExtra("Pagination", 4));
                break;
            case R.id.rl_coupon:
                startActivity(new Intent(PurchaseCode.this, CouponActivity.class));
                break;
            case R.id.btn:
                /*if ("1".equals(MyApplication.getInstance().userBean.getPayPassword())) {
                    startActivityForResult(new Intent(PurchaseCode.this,InPutPayPassWordActivity.class).putExtra("type",2),PAY_PASSWORK);
                } else {
                    showDialogv7(R.string.pay_toast, "取消", "去设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(PurchaseCode.this, PayPasswordActivity.class));
                        }
                    });
                }*/
                xianXiaPay();

                //startActivity(new Intent(PurchaseCode.this,ChoosePayActivity.class).putExtra("purchaseCodeResult",purchaseCodeResult));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case PAY_PASSWORK:
                if (resultCode == RESULT_OK){
                    /*String passwork = data.getStringExtra("passwork");
                    xianXiaPay(passwork);*/
                }

                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void xianXiaPay() {
        Map<String,Object> map = new HashMap<>();
        map.put("orderId",purchaseCodeResult.getOrderid());
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        Log.e(TAG, "xianXiaPay: "+orderId+"    "+ MyApplication.getInstance().userBean.getUserId());
        //map.put("payPass",passwork);
        showDialog();
        XUtil.Post(ConfigUtil.PAYXIANXIA,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    String info = json.optString("info");
                    MyToast(info);
                    if ("1".equals(re)){
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }
}
